﻿//3. We are given a matrix of strings of size N x M. Sequences in the matrix we define as sets of
//several neighbor elements located on the same line, column or diagonal.
//Write a program that finds the longest sequence of equal strings in the matrix. Example:


using System;
using System.Collections.Generic;

class LongestSequecesOfEqualStrings
{
    static void Main()
    {
        string[,] matrix = {
            {"ha", "fifi", "ho", "hi"},
            {"fo", "ha", "hi", "xx"},
            {"xxx", "ho", "ho", "xx"},
                           };

        List<string> equalElements = new List<string>();
        int longestSeq = 2;
        int longestRow = SearchByRows(matrix, longestSeq, equalElements);
        int longestCol = SearchByCol(matrix, longestRow, equalElements);
        int longestDiagonal = SearchByDiagonals(matrix, longestCol, equalElements);
        Print(longestDiagonal, equalElements);
    }

    static int Print(int longestSeq, List<string> equalElements)
    {
        if (equalElements.Count > 0)
        {
            Console.WriteLine("Longest sequence is {0} elements long by element(s):", longestSeq);
            foreach (var e in equalElements)
            {
                Console.Write("{0}, ", e);
            }
        }
        else
        {
            Console.WriteLine("There is no longest sequense");
        }
        return longestSeq;
    }

    static int SearchByRows(string[,] matrix, int longestSeq, List<string> equalElements)
    {
        int counter = 1;
        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            for (int col = 0; col < matrix.GetLength(1) - 1; col++)
            {
                if (matrix[row,col] == matrix[row,col + 1])
                {
                    counter++;
                    if (counter > longestSeq)
                    {
                        longestSeq = counter;
                        equalElements.Clear();
                        equalElements.Add(matrix[row, col]);
                    }
                    else if (counter == longestSeq)
                    {
                        equalElements.Add(matrix[row, col]);
                    }
                }
                else
                {
                    counter = 1;
                }
            }
        }
        return longestSeq;
    }

    static int SearchByCol(string[,] matrix, int longestSeq, List<string> equalElements)
    {
        int counter = 1;
        for (int col = 0; col < matrix.GetLength(1); col++)
        {
            for (int row = 0; row < matrix.GetLength(0) - 1; row++)
            {
                if (matrix[row, col] == matrix[row + 1, col])
                {
                    counter++;
                    if (counter > longestSeq)
                    {
                        longestSeq = counter;
                        equalElements.Clear();
                        equalElements.Add(matrix[row, col]);
                    }
                    else if (counter == longestSeq)
                    {
                        equalElements.Add(matrix[row, col]);
                    }
                }
                else
                {
                    counter = 1;
                }
            }
        }
        return longestSeq;
    }

    static int SearchByDiagonals(string[,] matrix, int longestSeq, List<string> equalElements)
    {
        int counter = 1;
        for (int row = 0; row < matrix.GetLength(0) - 1; row++)
        {
            int startCol = 0;
            while (row == 0 && startCol < matrix.GetLength(1) - 1)
            {
                for (int col = startCol; col < matrix.GetLength(1) - 1; col++)
                {
                    if (col - startCol + 1 == matrix.GetLength(0))
                    {
                        break;
                    }
                    if (matrix[row + col - startCol, col] == matrix[row + col + 1 - startCol, col + 1])
                    {
                        counter++;
                        if (counter > longestSeq)
                        {
                            longestSeq = counter;
                            equalElements.Clear();
                            equalElements.Add(matrix[row + col - startCol, col]);
                        }
                        else if (counter == longestSeq)
                        {
                            equalElements.Add(matrix[row + col - startCol, col]);
                        }
                    }
                    else
                    {
                        counter = 1;
                    }
                }
                startCol++;
            }
            if (row != 0)
            {
                for (int col = 0; col < matrix.GetLength(1) - 1; col++)
                {
                    if (row + col + 1 == matrix.GetLength(0))
                    {
                        break;
                    }
                    if (matrix[row + col, col] == matrix[row + col + 1, col + 1])
                    {
                        counter++;
                        if (counter > longestSeq)
                        {
                            longestSeq = counter;
                            equalElements.Clear();
                            equalElements.Add(matrix[row + col, col]);
                        }
                        else if (counter == longestSeq)
                        {
                            equalElements.Add(matrix[row + col, col]);
                        }
                    }
                    else
                    {
                        counter = 1;
                    }
                }
            }
        }
        return longestSeq;
    }
}
